/*
* Copyright (c) 2007 by Kirill Kolodyazhniy.
* See the file "license.terms" for information on usage and redistribution.
*/

#ifndef M_RESOURCEMANAGER_H
#define M_RESOURCEMANAGER_H

#include "log.h"
#include "refptr.h"
#include "types.h"
#include "singleton.h"
#include "resource.h"

#include <string>
#include <map>

namespace Engine
{
	class ResourceFactory;

	/**
	*   Base class for managing resources.
	*/
	class ResourceManager: public Singleton<ResourceManager>
	{
		typedef std::map<std::string, ResourceFactoryPtr> ResourceFactoryMap;
		typedef std::map<std::string, RefPtr<Resource, WeakPtrExtension> > ResourceMap;
	public:

		/**
		*   Constructor.
		*/
		ResourceManager();

		/**
		*   Destructor.
		*/
		virtual ~ResourceManager();

		/**
		*   Load resource.
		*   @param name file name.
		*/
		ResourcePtr Create(const std::string& filename, const std::string& type);

		/**
		*   Unload resource from memory.
		*/
		void Remove(const std::string& filename);

		/**
		*   Unload resource from memory only if there are no reference to it.
		*/
		void TryRemove(const std::string& filename);

		/**
		*   Reload all resources.
		*/
		void ReLoadAll();

		/**
		*   Unload all resources.
		*/
		void RemoveAll();

		/**
		*   Add factory for resource creation.
		*/
		void AddResourceFactory(ResourceFactoryPtr factory);

	private:

		ResourceMap m_resources;
		ResourceFactoryMap m_factories;

		RefPtr<MeshFactory, WeakPtrExtension> m_meshFactory;
		RefPtr<MaterialFactory, WeakPtrExtension> m_materailFactory;
		RefPtr<TextureFactory, WeakPtrExtension> m_textureFactory;
	};
}

#endif
